package com.cxhc.app.api;

import com.cxhc.system.domain.app.ApprovalFlow;
import com.cxhc.system.domain.app.GroupUser;
import com.cxhc.system.dto.ApprovalDto;
import com.cxhc.system.dto.EventApprovalDto;
import com.cxhc.app.api.vo.EventApprovalVo;
import com.cxhc.common.core.domain.R;
import com.cxhc.system.domain.app.EventApproval;
import com.cxhc.system.service.app.IApprovalFlowService;
import com.cxhc.system.service.app.IEventApprovalFileService;
import com.cxhc.system.service.app.IEventApprovalService;
import com.cxhc.system.service.app.IGroupUserService;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 事项报备审批
 */
@RestController
@RequestMapping("/api/eventApproval")
public class ApiEventApproval {
    @Resource
    private IEventApprovalService eventApprovalService;

    @Resource
    private IGroupUserService groupUserService;

    @Resource
    private IEventApprovalFileService approvalFileService;

    @Resource
    private IApprovalFlowService approvalFlowService;


    /**
     * 列表
     *
     * @param vo
     * @return
     */
    @GetMapping("/list")
    public R<List<EventApproval>> approvalList(EventApprovalVo vo) {
        return R.ok(eventApprovalService.selectEventApprovalList(new EventApproval().setTitle(vo.getName()).setStatus(vo.getStatus())));
    }

    /**
     * 列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/oldList")
    public R<List<EventApproval>> approvalOldList(String userId) {
        return R.ok(eventApprovalService.selectEventApprovalOldList(userId));
    }


    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R<Map<String,Object>> approvalInfo(@PathVariable Long id) {
        return R.ok(ImmutableMap.of("info", eventApprovalService.selectEventApprovalById(id),
                "approvalUserList",approvalFlowService.selectApprovalFlowListByEventApprovalId(id),
                "fileList",approvalFileService.selectEventApprovalFileListByEventApprovalId(id)));
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/oldInfo/{id}")
    public R<Map<String,Object>> approvalOldInfo(@PathVariable Long id) {
        EventApproval eventApproval = eventApprovalService.selectEventApprovalOldById(id);
        String[] fileUrls = eventApproval.getFile().split(",");
        return R.ok(ImmutableMap.of("info", eventApproval,
                "approvalUserList",approvalFlowService.selectApprovalFlowOldListByEventApprovalId(id),
                "fileList",fileUrls));
    }

    /**
     * 审批人员列表
     *
     * @return
     */
    @GetMapping("/approvalUserList")
    public R<List<GroupUser>> approvalUserList() {
        return R.ok(groupUserService.selectGroupUserList(new GroupUser()));
    }


    /**
     * 新增报备审批
     *
     * @param dto
     * @return
     */
    @PostMapping("/postEventApproval")
    private R<String> postEventApproval(@RequestBody @Valid EventApprovalDto dto) {
        int row = eventApprovalService.insert(dto);
        return row == 1 ? R.ok() : R.fail();
    }


    /**
     * 审批
     *
     * @param dto
     */
    @PutMapping("/approval")
    private R<String> approval(@RequestBody @Valid ApprovalDto dto) {
        EventApproval eventApproval = eventApprovalService.selectEventApprovalById(dto.getId());
        if (eventApproval == null) {
            return R.fail("事项信息不存在");
        }
        if (eventApproval.getStatus() == 2) {
            return R.fail("事项已审批");
        }
        int row = eventApprovalService.updateEventApproval( eventApproval.setStatus(dto.getStatus()).setContent(dto.getContent()));

        if(row == 1 && dto.getUserId() != null){
            approvalFlowService.insertApprovalFlow(new ApprovalFlow()
                    .setEventId(dto.getId())
                    .setUserId(dto.getUserId())
                    .setDeptName(dto.getDeptName())
                    .setUserName(dto.getUserName()));
        }
        return row == 1 ? R.ok() : R.fail();
    }


}
